package net.zjitc.service.impl;

import net.zjitc.mapper.CategoryMapper;
import net.zjitc.pojo.Category;
import net.zjitc.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper vueShopCategory;

    @Override

    public List<Category> findAll() {
        List<Category> shops = vueShopCategory.findAll();
        for (int i = 0; i < shops.size(); i++) {
            Category category =  shops.get(i);
            Integer pid = category.getPid();
            if(pid!=0){
                for (Category c:shops){
                    if(c.getId()==pid){
                        category.setPname(c.getName());
                    }
                }
            }
        }
        return shops;
    }

    @Override
    public void insertShop(Category category) {
        vueShopCategory.insertShop(category);
    }

    @Override
    public int delete(Integer id) {
        int i = vueShopCategory.delete(id);
        return 0;
    }

    @Override
    public Category findById(Integer id) {
       return vueShopCategory.findById(id);
    }

    @Override
    public void update(Category category) {
        vueShopCategory.update(category);
    }

    @Override
    public List<Category> findPid() {
        List<Category> pCategory = vueShopCategory.findPid();
       return pCategory;
    }

    @Override
    public List<Category> findByName(String name) {
        List<Category> category = vueShopCategory.findByName(name);
        return category;
    }
}
